<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            margin: 10px auto;
            display: flex;
            justify-content: space-between;
        }

        #aa {
            width: 150px;
            background-color: palegreen;
            border: 1px solid #ccc;
        }

        #btn {

            background-color: yellowgreen;
            border: 3px solid #ccc;
        }
    </style>
</head>

<body>
    <div class="box">
        <input type="text" id="aa">
        <input type="button" id="btn" value="是否为两位数">
    </div>
</body>
<script>
    var input = document.getElementsByTagName('input')

    for (var i = 0; i < input.length; i++) {
        input[0].onkeyup = function () {
            this.value = this.value.replace(/[^\d]/, '')
        }
    }

    input[1].onclick = function () {
        // 提取字符长度
        var v = input[0].value
        var l = v.length

        alert(l == 2 ? '\u2713是两位数' : '这是' + l + '位数')
    }
</script>

</html>